﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.CategoriesService;
using DDF.Entity;
using ARR.UI.ArticlesService;

namespace ARR.UI.Models
{
    public interface ITheoreticalMarginModel : IModelBase
    {
        ObservableCollection<TheoreticalMargin> TheoreticalMarginsForArticles { get; set; }

        ObservableCollection<TheoreticalMargin> TheoreticalMarginsForCategories { get; set; }

        ObservableCollection<Category> Categories { get; set; }

        ObservableCollection<Article> Articles { get; set; }

        void Save();

        void LoadTheoreticalMargins();
    }

    public class TheoreticalMarginModel : ModelBase, ITheoreticalMarginModel
    {
        public TheoreticalMarginModel()
        {
            TheoreticalMarginsForArticles = new ObservableCollection<TheoreticalMargin>();
            TheoreticalMarginsForCategories = new ObservableCollection<TheoreticalMargin>();
            Categories = new ObservableCollection<Category>();
            Articles = new ObservableCollection<Article>();

            LoadCategories();
            LoadArticles();


            LoadTheoreticalMargins();
        }


        public ObservableCollection<TheoreticalMargin> TheoreticalMarginsForArticles { get; set; }

        public ObservableCollection<TheoreticalMargin> TheoreticalMarginsForCategories { get; set; }

        public ObservableCollection<Category> Categories { get; set; }

        public ObservableCollection<Article> Articles { get; set; }

        public void Save()
        {
            List<TheoreticalMargin> toSave = TheoreticalMarginsForCategories.Where(x => x.GetEntityState() != EntityState.Original).ToList();
            toSave.AddRange(TheoreticalMarginsForArticles.Where(x => x.GetEntityState() != EntityState.Original));

            toSave.ForEach(x => x.CountryId = WorkspaceData.Instance.Country.Id);

            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                service.SaveTheoreticalMargins(toSave.ToList());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

        }

        public void LoadTheoreticalMargins()
        {
            List<TheoreticalMargin> margins = new List<TheoreticalMargin>();

            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                margins = service.GetTheoreticalMargins(WorkspaceData.Instance.Country.Id);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }


            foreach (TheoreticalMargin margin in margins)
            {
                if (margin.ArticleId.HasValue)
                {
                    margin.Article = Articles.SingleOrDefault(x => x.Id == margin.ArticleId.Value);
                }

                margin.ChangeState(EntityState.Original);
            }

            TheoreticalMarginsForArticles = new ObservableCollection<TheoreticalMargin>(margins.Where(m => m.ArticleId.HasValue));
            TheoreticalMarginsForCategories = new ObservableCollection<TheoreticalMargin>(margins.Where(m => m.CategoryId.HasValue));

        }

        private void LoadArticles()
        {
            ArticlesServiceClient service = new ArticlesServiceClient();

            try
            {
                Articles = new ObservableCollection<Article>(service.GetAll(WorkspaceData.Instance.RangeReview.Id, null, true));
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

        }

        private void LoadCategories()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();
            List<Category> categories = new List<Category>();

            try
            {
                categories = service.GetByLevels(2);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            foreach (var category in categories.Where(x => x.Level == 2))
            {
                category.SubCategories = new List<Category>();
                category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
            }
            Categories = new ObservableCollection<Category>(categories.Where(x => x.Level == 2).OrderBy(x => x.Name));




        }
    }

    public class TheoreticalMarginModelMock : MockModelBase, ITheoreticalMarginModel
    {
        public TheoreticalMarginModelMock()
        {
            TheoreticalMarginsForArticles = new ObservableCollection<TheoreticalMargin>();
            TheoreticalMarginsForCategories = new ObservableCollection<TheoreticalMargin>();
            Categories = new ObservableCollection<Category>();
            Articles = new ObservableCollection<Article>();

            LoadTheoreticalMargins();
            LoadCategories();
            LoadArticles();
        }

        public ObservableCollection<TheoreticalMargin> TheoreticalMarginsForArticles { get; set; }

        public ObservableCollection<TheoreticalMargin> TheoreticalMarginsForCategories { get; set; }

        public ObservableCollection<Category> Categories { get; set; }

        public ObservableCollection<Article> Articles { get; set; }

        public void Save()
        {

        }

        public void LoadTheoreticalMargins()
        {
            TheoreticalMarginsForCategories.Add(new TheoreticalMargin { CategoryId = 1, CountryId = 1, Margin = 0.358M });
            TheoreticalMarginsForCategories.Add(new TheoreticalMargin { CategoryId = 2, CountryId = 1, Margin = 0.358M });
            TheoreticalMarginsForCategories.Add(new TheoreticalMargin { CategoryId = 3, CountryId = 1, Margin = 0.358M });
            TheoreticalMarginsForCategories.Add(new TheoreticalMargin { CategoryId = 4, CountryId = 1, Margin = 0.358M });
        }

        private void LoadArticles()
        {
            Articles.Clear();

            Articles.Add(new Article { Id = 1, Code = "AR01", EAN = "AR01", Name = "Articulo 1", Brand = "Brand", Manufacturer = "Manufacturer" });
            Articles.Add(new Article { Id = 2, Code = "AR02", EAN = "AR02", Name = "Articulo 2", Brand = "Brand", Manufacturer = "Manufacturer" });
            Articles.Add(new Article { Id = 3, Code = "AR03", EAN = "AR03", Name = "Articulo 3", Brand = "Brand", Manufacturer = "Manufacturer" });
            Articles.Add(new Article { Id = 4, Code = "AR04", EAN = "AR04", Name = "Articulo 4", Brand = "Brand", Manufacturer = "Manufacturer" });
            Articles.Add(new Article { Id = 5, Code = "AR05", EAN = "AR05", Name = "Articulo 5", Brand = "Brand", Manufacturer = "Manufacturer" });
            Articles.Add(new Article { Id = 6, Code = "AR06", EAN = "AR06", Name = "Articulo 6", Brand = "Brand", Manufacturer = "Manufacturer" });
            Articles.Add(new Article { Id = 7, Code = "AR07", EAN = "AR07", Name = "Articulo 7", Brand = "Brand", Manufacturer = "Manufacturer" });

            //foreach (Article art in Articles)
            //{
            //    art.GetRow().ChangeState(EntityState.Original);
            //}
        }

        private void LoadCategories()
        {
            Categories.Add(new Category { Id = 1, Code = "Cat01", Name = "Categoria 1", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 2, Code = "Cat02", Name = "Categoria 2", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 3, Code = "Cat03", Name = "Categoria 3", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 4, Code = "Cat04", Name = "Categoria 4", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 5, Code = "Cat05", Name = "Categoria 5", Level = 1, Convention = "NACS" });
        }
    }

}
